package mof.gov.dmdc.view.backing.coamonitor;

import java.util.Date;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.servlet.http.HttpSession;

import mof.gov.dmdc.model.applicationmodule.extra.COAExtImpl;
import mof.gov.dmdc.model.applicationmodule.extra.COAModuleImpl;
import mof.gov.dmdc.model.applicationmodule.extra.TabmisIntImpl;
import mof.gov.dmdc.view.common.Common;

import mof.gov.dmdc.view.common.Formater;

import oracle.jbo.Row;
import oracle.jbo.ViewObject;


public class COAMonitor {
    private String pSegment = "Fund Type";
    private String pcodeToFind;
    private String pSegmentName = "Fund Type";

    private Date startDate = new Date();
    private Date endDate = new Date();

    public COAMonitor() {
        super();
    }

    public void setPSegment(String psegment) {
        this.pSegment = psegment;
    }

    public String getPSegment() {
        return pSegment;
    }

    public void setPcodeToFind(String pcodeToFind) {
        this.pcodeToFind = pcodeToFind;
    }

    public String getPcodeToFind() {
        return pcodeToFind;
    }

    public void setPSegmentName(String pSegmentName) {
        this.pSegmentName = pSegmentName;
    }

    public String getPSegmentName() {
        return pSegmentName;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    private void doCheck(String codeDelim, String pSegment, String sessId) {
        String[] codetoFind = codeDelim.split(";");
        String tmpStr = "", tmpTen = "";
        String vCode = "";
        String sDNS = "", sTMP = "", sDMDC = "", sDMDCEXT = "", sDMDCTAB = "";
        String dmTable = "";

        COAModuleImpl am = (COAModuleImpl)Common.getAppModuleForDataControl("COAModuleDataControl");
        for (int i = 0; i < codetoFind.length; i++) {
            vCode = codetoFind[i];
            if (pSegment.equals("Fund Type")) {
                dmTable = "dm_quy";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";

            } else if (pSegment.equals("Natural Account")) {
                dmTable = "dm_tkkt";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";


            } else if (pSegment.equals("Economic Code")) {
                dmTable = "dm_ndkt";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Budget Level")) {
                dmTable = "dm_capns";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Organization")) {
                dmTable = "dm_dvqhns";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten_qhns from " + dmTable + " where ma_qhns='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = am.getScalar("select ten from tmp_dvqhns_changes where ma='" + vCode + "' and rownum < 2");
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sTMP = "1";
                else
                    sTMP = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Location")) {
                dmTable = "dm_dbhc";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Chapter")) {
                dmTable = "dm_chuong";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Function Code")) {
                dmTable = "dm_nganhkt";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Target Program/Project")) {
                dmTable = "dm_ctmt";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Treasury")) {
                dmTable = "dm_kbnn";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Source")) {
                dmTable = "dm_nguonchi";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            } else if (pSegment.equals("Spare Segment")) {
                dmTable = "dm_duphong";
                sDNS = "";
                sTMP = "";
                sDMDC = "0";
                sDMDCEXT = "0";
                sDMDCTAB = "0";

                tmpTen = am.getScalar("select ten from " + dmTable + " where ma='" + vCode + "' and rownum < 2");
                if (!tmpTen.equals("NODATA") && !tmpTen.equals("ERROR") && !tmpTen.equals(""))
                    sDMDC = "1";
                else
                    sDMDC = "0";

                tmpStr = getTenEXT(vCode, pSegment);
                if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                    sDMDCEXT = "1";
                else
                    sDMDCEXT = "0";
            }

            String tmpTuNgay = "", tmpDenNgay = "";

            if (pSegment.equals("Organization")) {
                tmpTuNgay = am.getScalar("select to_char(ngay_ps, 'dd/MM/yyyy') tu from " + dmTable + " where ma_qhns = '" + vCode + "'");
                tmpDenNgay = am.getScalar("select to_char(ngay_kt, 'dd/MM/yyyy') tu from " + dmTable + " where ma_qhns = '" + vCode + "'");
            } else {
                tmpTuNgay = am.getScalar("select to_char(ngay_ps, 'dd/MM/yyyy') tu from " + dmTable + " where ma = '" + vCode + "'");
                tmpDenNgay = am.getScalar("select to_char(ngay_kt, 'dd/MM/yyyy') tu from " + dmTable + " where ma = '" + vCode + "'");
            }

            tmpStr = getTenTabmis(vCode, pSegment, "Description");
            if (!tmpStr.equals("NODATA") && !tmpStr.equals("ERROR") && !tmpStr.equals(""))
                sDMDCTAB = "1";
            else
                sDMDCTAB = "0";
            String vStatus = "", vErrCode = "", vErrDesc = "";

            vStatus = getTenTabmis(vCode, pSegment, "Status");
            vErrCode = getTenTabmis(vCode, pSegment, "ErrorCode");
            vErrDesc = getTenTabmis(vCode, pSegment, "ErrorDescription");
            am.insertCheckData(vCode, tmpTen, tmpTuNgay, tmpDenNgay, vStatus, sDNS, sTMP, sDMDC, sDMDCEXT, sDMDCTAB, vErrCode, vErrDesc, "", pSegment, sessId);

        }


    }

    private COAExtImpl getCOAExtAppModule() {
        return (COAExtImpl)Common.getAppModuleForDataControl("COAExtDataControl");
    }

    private TabmisIntImpl getTabmisIntAppModule() {
        return (TabmisIntImpl)Common.getAppModuleForDataControl("TabmisIntDataControl");
    }

    private String getTenTabmis(String ma, String pSegment, String vField) {
        TabmisIntImpl amTm = getTabmisIntAppModule();
        ViewObject mView = null;

        mView = amTm.getTabmisValueSetIntView();

        try {
            mView.setNamedWhereClauseParam("pMa", ma);
            mView.setNamedWhereClauseParam("pSegment", pSegment);
            mView.executeQuery();
        } catch (Exception expoi) {
            expoi.printStackTrace();
            mView.cancelQuery();
        }


        String retVal = "", tmpMa = "";
        Row ordStr = mView.first();
        try {

            tmpMa = ordStr.getAttribute("FlexValueDisp").toString();

            if (tmpMa.equals(ma)) {
                retVal = ordStr.getAttribute(vField).toString();
            }
            ordStr = mView.next();
        } catch (NullPointerException nprw) {
            // do nothing
            retVal = "";
        } catch (Exception e) {
            // do nothing
            retVal = "";
        }
        return retVal;
    }

    private String getTenEXT(String ma, String pSegment) {
        COAExtImpl amExt = getCOAExtAppModule();
        ViewObject mView = null;
        if (pSegment.equals("Fund Type")) {
            mView = amExt.getDmQuyView();
        } else if (pSegment.equals("Natural Account")) {
            mView = amExt.getDmTkktView();
        } else if (pSegment.equals("Economic Code")) {
            mView = amExt.getDmNdktView();
        } else if (pSegment.equals("Budget Level")) {
            mView = amExt.getDmCapNSView();
        } else if (pSegment.equals("Organization")) {
            mView = amExt.getDmDvqhnsTrungGianView();
        } else if (pSegment.equals("Location")) {
            mView = amExt.getDmDbhcView();
        } else if (pSegment.equals("Chapter")) {
            mView = amExt.getDmChuongView();
        } else if (pSegment.equals("Function Code")) {
            mView = amExt.getDmNganhKTView();
        } else if (pSegment.equals("Target Program/Project")) {
            mView = amExt.getDMCtmtView();
        } else if (pSegment.equals("Treasury")) {
            mView = amExt.getDmKbnnView();
        } else if (pSegment.equals("Source")) {
            mView = amExt.getDmNguonChiView();
        } else if (pSegment.equals("Spare Segment")) {
            mView = amExt.getDmDuPhongView();
        }

        try {
            mView.setNamedWhereClauseParam("pMa", ma);
            mView.executeQuery();
        } catch (Exception expoi) {
            expoi.printStackTrace();
            mView.cancelQuery();
        }


        String retVal = "", tmpMa = "";
        Row ordStr = mView.first();
        try {

            tmpMa = ordStr.getAttribute("Ma").toString();

            if (tmpMa.equals(ma)) {
                retVal = ordStr.getAttribute("Ten").toString();
            }
            ordStr = mView.next();
        } catch (NullPointerException nprw) {
            retVal = "";
        } catch (Exception e) {
            retVal = "";
        }
        return retVal;
    }

    public void checkAction(ActionEvent actionEvent) {
        if (pcodeToFind != null && !pcodeToFind.isEmpty()) {
            FacesContext ctx = FacesContext.getCurrentInstance();
            ExternalContext ectx = ctx.getExternalContext();
            HttpSession session = (HttpSession)ectx.getSession(false);
            String sessionId = session.getId();
            COAModuleImpl am = (COAModuleImpl)Common.getAppModuleForDataControl("COAModuleDataControl");
            am.deleteSessionData(sessionId);
            doCheck(pcodeToFind, pSegment, sessionId);
            try {
                ViewObject tblView = Common.getViewObjectForIterator("COAMoniterIterator");
                tblView.setNamedWhereClauseParam("pSessionId", sessionId);
                tblView.executeQuery();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }        
    }

    public void filterAction(ActionEvent actionEvent) {
        String d1 = null;
        String d2 = null;
        if (startDate != null) {
            d1 = Formater.date2str(startDate);
        } else {
            d1 = "01/01/1900";
        }
        if (endDate != null) {
            d2 = Formater.date2str(endDate);
        } else {
            d2 = "01/01/2900";
        }
        try {
            ViewObject tblView = Common.getViewObjectForIterator("TabmisValueSetIntView2Iterator");
            tblView.setNamedWhereClauseParam("pSegment", pSegmentName);
            tblView.setNamedWhereClauseParam("pStartDate", d1);
            tblView.setNamedWhereClauseParam("pEndDate", d2);
            tblView.executeQuery();
            tblView.clearCache();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
